/*
	system_exceptions.c

	contains exception controller code

	Author: Aidan Goddard 26/12/13
*/

#include"headers/CPU_exception.h"
#include"headers/system_error.h"
#include"headers/bakery_lock.h"

#include"../setup/headers/printf.h"




///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
// Double fault handler routine

static void HandleDF_8(struct REGISTER_STATE *regs)
{
	// a double fault really should not have happened

	// generate the error message

}


///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
// main exception handler routine
static SCH_CLS exception_lock;
struct REGISTER_STATE *CPUExceptionHandler(struct REGISTER_STATE *regs)
{
	// grab lock on exception system
	BakeryEnter(&exception_lock, GetCPUID());

	// TEMP - signal kernel error
	KERNEL_EXCEPTION(regs);



	// check which exception occured

	// finished, release lock and return
	BakeryLeave(&exception_lock, GetCPUID());
	return regs;

/*

	printf("\nEXCEPTION OCCURED");

	printf("\nRSP: 0x%a", regs->RSP);
	printf("\nRIP: 0x%a", regs->RIP);
	printf("\nCR2: 0x%a", regs->CR2);
	printf("\nCR3: 0x%a", regs->CR3);
	printf("\nERROR CODE: 0x%a", regs->ERROR_CODE);
	printf("\nEXCEPTION: 0x%a", regs->EXCEPTION);

	__asm__ __volatile__("cli; hlt;":::"memory");
*/

}
